
#include "Viewer.h"

#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkSmartPointer.h"
#include "vtkObjectFactory.h"
#include "vtkContext2D.h"
#include "vtkContextItem.h"
#include "vtkContextActor.h"
#include "vtkContextScene.h"
#include "vtkPen.h"
#include "vtkBrush.h"
#include "vtkTextProperty.h"
#include "vtkOpenGLContextDevice2D.h"
#include "vtkStdString.h"
#include "vtkContextInteractorStyle.h"


Viewer::Viewer()
{
}


Viewer::~Viewer()
{
}


void
Viewer::AddChart(Chart *chart)
{
	_charts.push_back(chart);
}


void
Viewer::RemoveAllCharts()
{
	_charts.clear();
}


void
Viewer::View()
{
	// **************************************************************
	// TODO: este metodo esta feito para funcionar so no meu codigo.
	// TODO: Consertar URGENTE!
	// **************************************************************

	vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
	vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor = vtkSmartPointer<vtkRenderWindowInteractor>::New();

	vtkSmartPointer<vtkRenderWindow> renderWindow2 = vtkSmartPointer<vtkRenderWindow>::New();
	vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor2 = vtkSmartPointer<vtkRenderWindowInteractor>::New();

	renderWindow->SetSize(700, 300);
	renderWindow2->SetSize(700, 300);

	renderWindow->SetPosition(500, 50);
	renderWindow2->SetPosition(500, 380);

	renderWindowInteractor->SetRenderWindow(renderWindow);
	renderWindowInteractor2->SetRenderWindow(renderWindow2);

	vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
	vtkSmartPointer<vtkRenderer> renderer2 = vtkSmartPointer<vtkRenderer>::New();

	vtkSmartPointer<vtkContextActor> actor = vtkSmartPointer<vtkContextActor>::New();
	actor->GetScene()->AddItem(_charts[0]->BuildChart());

	vtkSmartPointer<vtkContextActor> actor2 = vtkSmartPointer<vtkContextActor>::New();
	actor2->GetScene()->AddItem(_charts[1]->BuildChart());

	renderer2->AddActor(actor2);
	renderer->AddActor(actor);

	renderWindow->AddRenderer(renderer);
	renderWindow2->AddRenderer(renderer2);

	vtkContextInteractorStyle* style = vtkContextInteractorStyle::New();
	style->SetScene(actor->GetScene());
	renderWindowInteractor->SetInteractorStyle(style);
	style->Delete();
	renderer->SetBackground(1.0, 1.0, 1.0);

	vtkContextInteractorStyle* style2 = vtkContextInteractorStyle::New();
	style2->SetScene(actor2->GetScene());
	renderWindowInteractor2->SetInteractorStyle(style2);
	style2->Delete();
	renderer2->SetBackground(1.0, 1.0, 1.0);

	renderWindow->Render();
	renderWindow2->Render();
	renderWindowInteractor->Start();

//	vtkSmartPointer<vtkContextView> view = vtkSmartPointer<vtkContextView>::New();
//
//	view->GetRenderer()->SetViewport(0, 0, 1, 0.5);
//	view->GetScene()->AddItem(_charts[0]->BuildChart());
//	view->Render();
//	view->GetRenderer()->ResetCamera();
//
//	vtkSmartPointer<vtkRenderer> renderer2 = vtkSmartPointer<vtkRenderer>::New();
//	renderWindow->AddRenderer(renderer2);
//
//	view->GetRenderer()->SetViewport(0, 0.5, 1, 1);
//	view->GetScene()->AddItem(_charts[1]->BuildChart());
//	view->Render();
//	view->GetInteractor()->Initialize();
//	view->GetInteractor()->Start();

	// view->GetScene()->ClearItems();

//	for (int i = 0; i < _charts.size(); i++)
//	{
//		views.push_back(vtkSmartPointer<vtkContextView>::New());
//		views[views.size() - 1]->GetRenderWindow()->SetPosition(i * 100, i * 100);
//		views[views.size() - 1]->GetScene()->AddItem(_charts[i]->BuildChart());
//		views[views.size() - 1]->GetInteractor()->Initialize();
//	}

//	views[views.size() - 1]->GetInteractor()->Start();

//	for (int i = 0; i < _charts.size(); i++)
//	{
//		views[i]->GetScene()->RemoveItem((unsigned int) 0);
//		views[i]->GetRenderWindow()->Finalize();
////		views[i]->GetRenderWindow()->Delete();
////		views[i]->SetRenderWindow(NULL);
//		views[i]->GetInteractor()->TerminateApp();
//	}
}

